/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rbh.risk.managedbean.frontend;

import com.rbh.risk.entity.Privilege;
import com.rbh.risk.entity.Profile;
import com.rbh.risk.entity.RiskData;
import com.rbh.risk.entity.Workgroup;
import com.rbh.risk.service.AuthenticationService;
import com.rbh.risk.service.PrivilegeService;
import com.rbh.risk.util.Constants;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "privilegeBean")
@SessionScoped
public class PrivilegeBean implements Serializable {

    private static final long serialVersionUID = 1L;
    @ManagedProperty(value = "#{authenticationService}")
    private AuthenticationService authenticationService;
    @ManagedProperty(value = "#{privilegeService}")
    private PrivilegeService privilegeService;
    private Profile profile;
    private HashMap<Privilege, String> hashMapACL;

    public PrivilegeBean() {
    }

    @PostConstruct
    public void init() {
        profile = authenticationService.getPrincipal().getProfile();
        List<Privilege> dataList = privilegeService.getByRole(profile.getRole());
        hashMapACL = new HashMap<>();
        for (Iterator it = dataList.iterator(); it.hasNext();){
            Privilege p = (Privilege) it.next();
            hashMapACL.put(p, p.getAcl().getAclName());
        }
    }

    public boolean canEditRisk(RiskData risk) {
        return canDeleteRisk(risk);
    }

    public boolean canDeleteRisk(RiskData risk) {
        try {
            if (hashMapACL.containsValue(Constants.$_YOURSELF)
                    && (risk.getProfileReviewLevel1() == null)
                    && (risk.getProfileReviewLevel2() == null)
                    && (risk.getProfileReviewLevel3() == null)) {
                return true;
            } else {
                return this.canReview(risk);
            }
        } catch (Exception ex) {
            return false;
        }
    }

    public boolean isWorkgroup(RiskData risk) {
        try {
            if (hashMapACL.containsValue(Constants.$_YOURSELF)) {
                if (profile.getWorkgroupList().containsAll(risk.getProfileCreate().getWorkgroupList())) {
                    return true;
                }
                return false;
            }
            if (hashMapACL.containsValue(Constants.$_FRONTEND)) {
                return true;
            } else if (hashMapACL.containsValue(Constants.$_REVIEW_LV1)) {
                if (profile.getWorkgroupList().contains(risk.getWorkgroup())
                        || profile.getWorkgroupList().containsAll(risk.getProfileCreate().getWorkgroupList())) {
                    return true;
                }
                return false;
            } else if (hashMapACL.containsValue(Constants.$_REVIEW_LV2)) {
                for (Workgroup w : profile.getWorkgroupList()) {
                    if (w.getWorkgroupList().contains(risk.getWorkgroup())
                            || w.getWorkgroupList().containsAll(risk.getProfileCreate().getWorkgroupList())) {
                        return true;
                    }
                }
                return false;
            } else {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    public boolean canReview(RiskData risk) {
        try {
            boolean lv1 = (risk.getProfileReviewLevel1() == null);
            boolean lv2 = (risk.getProfileReviewLevel2() == null);
            boolean lv3 = (risk.getProfileReviewLevel3() == null);
            if (hashMapACL.containsValue(Constants.$_YOURSELF)) {
                return false;
            } else if ((hashMapACL.containsValue(Constants.$_REVIEW_LV1) && lv1) && (lv2 && lv3)) {
                return true;
            } else if ((hashMapACL.containsValue(Constants.$_REVIEW_LV2) && lv2) && lv3) {
                return true;
            } else if ((hashMapACL.containsValue(Constants.$_REVIEW_LV3)
                    || hashMapACL.containsValue(Constants.$_FRONTEND)) && lv3) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    public boolean cancelReview(RiskData risk) {
        try {
            boolean lv1 = (risk.getProfileReviewLevel1() == null);
            boolean lv2 = (risk.getProfileReviewLevel2() == null);
            boolean lv3 = (risk.getProfileReviewLevel3() == null);
            if (hashMapACL.containsValue(Constants.$_YOURSELF)) {
                return false;
            } else if ((hashMapACL.containsValue(Constants.$_REVIEW_LV1) && !lv1) && (lv2 && lv3)) {
                return true;
            } else if ((hashMapACL.containsValue(Constants.$_REVIEW_LV2) && !lv2) && lv3) {
                return true;
            } else if ((hashMapACL.containsValue(Constants.$_REVIEW_LV3)
                    || hashMapACL.containsValue(Constants.$_FRONTEND)) && !lv3) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    public RiskData setReviewLevel(RiskData risk) {
        if (hashMapACL.containsValue(Constants.$_REVIEW_LV1)) {
            risk.setProfileReviewLevel1(profile);
        } else if (hashMapACL.containsValue(Constants.$_REVIEW_LV2)) {
            risk.setProfileReviewLevel2(profile);
        } else if (hashMapACL.containsValue(Constants.$_REVIEW_LV3)
                || hashMapACL.containsValue(Constants.$_FRONTEND)) {
            risk.setProfileReviewLevel3(profile);
        }
        return risk;
    }

    public RiskData setUnReviewLevel(RiskData risk) {
        if (hashMapACL.containsValue(Constants.$_REVIEW_LV1)) {
            risk.setProfileReviewLevel1(null);
        } else if (hashMapACL.containsValue(Constants.$_REVIEW_LV2)) {
            risk.setProfileReviewLevel2(null);
        } else if (hashMapACL.containsValue(Constants.$_REVIEW_LV3)
                || hashMapACL.containsValue(Constants.$_FRONTEND)) {
            risk.setProfileReviewLevel3(null);
        }
        return risk;
    }

    public boolean backend() {
        return hashMapACL.containsValue(Constants.$_BACKEND);
    }

    public boolean yourSelf() {
        return hashMapACL.containsValue(Constants.$_YOURSELF);
    }

    public boolean viewReport() {
        boolean result = false;
        if (hashMapACL.containsValue(Constants.$_FRONTEND)
                || hashMapACL.containsValue(Constants.$_REPORT)) {
            result = true;
        }
        return result;
    }

    public AuthenticationService getAuthenticationService() {
        return authenticationService;
    }

    public void setAuthenticationService(AuthenticationService authenticationService) {
        this.authenticationService = authenticationService;
    }

    public PrivilegeService getPrivilegeService() {
        return privilegeService;
    }

    public void setPrivilegeService(PrivilegeService privilegeService) {
        this.privilegeService = privilegeService;
    }

    public HashMap<Privilege, String> getHashMapACL() {
        return hashMapACL;
    }

    public void setHashMapACL(HashMap<Privilege, String> hashMapACL) {
        this.hashMapACL = hashMapACL;
    }

    public Profile getProfile() {
        return profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }
}